import { Provide } from '@midwayjs/core'
import { InjectEntityModel } from '@midwayjs/typegoose'
import { ReturnModelType } from '@typegoose/typegoose'
import { WorkflowDataOptions } from '../../interface.js'
import { WorkflowData } from '../../entity/examples/workflowData.js'

@Provide()
export class WorkflowDataService {
  @InjectEntityModel(WorkflowData)
  workflowDataModel: ReturnModelType<typeof WorkflowData>

  async insertMany(data: any[]): Promise<any> {
    return this.workflowDataModel.insertMany(data)
  }

  async find(data): Promise<WorkflowDataOptions[]> {
    return this.workflowDataModel.find(data).exec()
  }

  async findOne(data) {
    return this.workflowDataModel.findOne(data).exec()
  }

  async updateOne(data): Promise<any> {
    const _id = data._id
    Reflect.deleteProperty(data, '_id')
    return this.workflowDataModel.updateOne({ _id }, { $set: data }).exec()
  }

  async deleteOne(data): Promise<any> {
    return await this.workflowDataModel.deleteOne(data).exec()
  }
}
